home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 9882 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.5 KB  |  70 lines

  1. Path: das-news2.harvard.edu!das-news!tlb
  2. From: tlb@chardonnay.eecs.harvard.edu (Trevor Blackwell)
  3. Newsgroups: comp.lang.c++
  4. Subject: parsing inline method definitions
  5. Date: 04 Mar 1996 19:12:42 -0500
  6. Organization: Harvard
  7. Sender: tlb@chardonnay.eecs.harvard.edu
  8. Message-ID: <vqevikk8kjo.fsf@chardonnay.eecs.harvard.edu>
  9. Reply-To: tlb@eecs.harvard.edu
  10. NNTP-Posting-Host: chardonnay.harvard.edu
  11. X-Disclaimer: 
  12.  
  13.  
  14. Is it possible to construct parse trees of inline method declarations
  15. at the time they are read?
  16.  
  17. Here's an example to show why it might be hard. If an instance
  18. variable 'foo' shadows a class name 'foo', it changes the parsing of
  19. 'foo *x'. This declaration of the instance variables can follow the
  20. inline declaration.
  21.  
  22. I can't believe the two would be represented the same in a parse tree.
  23.  
  24. I guess you could save the inline declaration as a pure token stream,
  25. and then parse it later. What does, say gcc do? (I don't want to read
  26. the code.)
  27.  
  28. The code below prints 8 and 1 on my system (gcc alpha-OSF1-V3.0).
  29.  
  30. class foo {
  31. public:
  32. };
  33.  
  34. class bar1 {
  35. public:
  36.   func() {
  37.     char x;
  38.     {
  39.       foo * x;           // x is pointer to foo
  40.       printf("%d\n",sizeof(x));
  41.     }
  42.   }
  43. };
  44.  
  45. class bar2 {
  46. public:
  47.   func() {
  48.     char x;
  49.     {
  50.       foo * x;          // multiply foo by x
  51.       printf("%d\n",sizeof(x));
  52.     }
  53.   }
  54.   int foo;              // shadows class foo
  55. };
  56.  
  57. main()
  58. {
  59.   bar1 b1;
  60.   bar2 b2;
  61.  
  62.   b1.func();
  63.   b2.func();
  64. }
  65.  
  66. -- 
  67. --
  68. Trevor Blackwell         tlb@eecs.harvard.edu          (617) 495-8912
  69. http://www.eecs.harvard.edu/~tlb
  70.